From 3b00f56485a70687bb401ee97473501f643f3f1a Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 19 Sep 2017 17:32:52 +0100
Subject: [PATCH] Search tags as well as pages

---
 Changelog.md               |  1 +
 build/index.php            | 25 +++++++++++++++++++++++--
 module_index.json          |  8 ++++----
 modules/feature-search.php | 23 ++++++++++++++++++++++-
 4 files changed, 50 insertions(+), 7 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index fbfbb4c..e7e47e6 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -23,6 +23,7 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t
 ### Changed
  - Make `invindex-rebuild` admin-only, but allow access if the POST param `secret` is set to your secret in `peppermint.json`
  - Improved the wording of the deletion confirmation and error pages
+ - Search now searches matching tags as well as pages
 
 ## v0.14
 
diff --git a/build/index.php b/build/index.php
index 6695f5e..7287479 100644
--- a/build/index.php
+++ b/build/index.php
@@ -3420,7 +3420,7 @@ register_module([
 
 register_module([
 	"name" => "Search",
-	"version" => "0.5.1",
+	"version" => "0.6",
 	"author" => "Starbeamrainbowlabs",
 	"description" => "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.",
 	"id" => "feature-search",
@@ -3584,6 +3584,27 @@ register_module([
 			}
 			$content .= "</p>";
 			
+			if(module_exists("page-list")) {
+				$nterms = search::tokenize($query);
+				$nterms_regex = implode("|", array_map(function($nterm) {
+					return preg_quote(strtolower(trim($nterm)));
+				}, $nterms));
+				$all_tags = get_all_tags();
+				$matching_tags = [];
+				foreach($all_tags as $tag) {
+					if(preg_match("/$nterms_regex/i", trim($tag)) > 0)
+						$matching_tags[] = $tag;
+				}
+				
+				if(count($matching_tags) > 0) {
+					$content .= "<p>Matching tags: <span class='tags'>";
+					foreach($matching_tags as $tag) {
+						$content .= "\t<a href='?action=list-tags&tag=" . rawurlencode($tag)  ."' class='mini-tag'>" . htmlentities($tag) . "</a> \n";
+					}
+					$content .= "</span></p>";
+				}
+			}
+			
 			$i = 0; // todo use $_GET["offset"] and $_GET["result-count"] or something
 			foreach($results as $result)
 			{
@@ -6522,7 +6543,7 @@ register_module([
 
 register_module([
 	"name" => "Page list",
-	"version" => "0.10.4",
+	"version" => "0.11",
 	"author" => "Starbeamrainbowlabs",
 	"description" => "Adds a page that lists all the pages in the index along with their metadata.",
 	"id" => "page-list",
diff --git a/module_index.json b/module_index.json
index 57e637d..7346a05 100755
--- a/module_index.json
+++ b/module_index.json
@@ -100,11 +100,11 @@
     },
     {
         "name": "Search",
-        "version": "0.5.1",
+        "version": "0.6",
         "author": "Starbeamrainbowlabs",
         "description": "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.",
         "id": "feature-search",
-        "lastupdate": 1503747477,
+        "lastupdate": 1505838750,
         "optional": false
     },
     {
@@ -190,11 +190,11 @@
     },
     {
         "name": "Page list",
-        "version": "0.10.4",
+        "version": "0.11",
         "author": "Starbeamrainbowlabs",
         "description": "Adds a page that lists all the pages in the index along with their metadata.",
         "id": "page-list",
-        "lastupdate": 1505769039,
+        "lastupdate": 1505769119,
         "optional": false
     },
     {
diff --git a/modules/feature-search.php b/modules/feature-search.php
index 64cba34..b822154 100644
--- a/modules/feature-search.php
+++ b/modules/feature-search.php
@@ -1,7 +1,7 @@
 <?php
 register_module([
 	"name" => "Search",
-	"version" => "0.5.1",
+	"version" => "0.6",
 	"author" => "Starbeamrainbowlabs",
 	"description" => "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.",
 	"id" => "feature-search",
@@ -165,6 +165,27 @@ register_module([
 			}
 			$content .= "</p>";
 			
+			if(module_exists("page-list")) {
+				$nterms = search::tokenize($query);
+				$nterms_regex = implode("|", array_map(function($nterm) {
+					return preg_quote(strtolower(trim($nterm)));
+				}, $nterms));
+				$all_tags = get_all_tags();
+				$matching_tags = [];
+				foreach($all_tags as $tag) {
+					if(preg_match("/$nterms_regex/i", trim($tag)) > 0)
+						$matching_tags[] = $tag;
+				}
+				
+				if(count($matching_tags) > 0) {
+					$content .= "<p>Matching tags: <span class='tags'>";
+					foreach($matching_tags as $tag) {
+						$content .= "\t<a href='?action=list-tags&tag=" . rawurlencode($tag)  ."' class='mini-tag'>" . htmlentities($tag) . "</a> \n";
+					}
+					$content .= "</span></p>";
+				}
+			}
+			
 			$i = 0; // todo use $_GET["offset"] and $_GET["result-count"] or something
 			foreach($results as $result)
 			{
